﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace BBPA.Admin
{
    /// <summary>
    /// daoru 的摘要说明
    /// </summary>
    public class xml2category : IHttpHandler
    {
        BLL.Category bll = new BLL.Category();
        int allCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            return;
            context.Response.ContentType = "text/plain";
            string filepath = System.Web.HttpContext.Current.Request.MapPath("/xml/category.xml");
            XmlDocument doc = new System.Xml.XmlDocument();
            try
            {
                doc.Load(filepath);
            }
            catch (InvalidCastException e) { }

            int categoryID1 = 0, categoryID2 = 0;
            Model.Category model1 = new Model.Category();
            Model.Category model2 = new Model.Category();
            
            foreach (XmlNode row in doc.SelectNodes("/Worksheet/Table/Row"))
            {
                XmlNodeList listCell = row.SelectNodes("Cell");
                int FirstIndex =listCell[0].Attributes["Index"]==null?0: DataTypePlus.requestInt(listCell[0].Attributes["Index"].Value);
                int delN = FirstIndex == 0 ? 0 : FirstIndex - 1;
                //
                if (FirstIndex==0&&listCell[0] != null && listCell[0].InnerText != "")
                {
                    if (listCell[1].InnerText == "凡士林")
                    {

                        int k = 1;
                    }
                    model1 = bll.GetModel(AddCategory(listCell[1].InnerText, new Model.Category()));
                    context.Response.Write(listCell[1].InnerText);
                }
                XmlNode cell2 = listCell[3 - delN];
                if (cell2 != null && cell2.InnerText != "")
                {
                    model2 = bll.GetModel(AddCategory(cell2.InnerText, model1));
                    context.Response.Write(" - " + cell2.InnerText);
                }
                XmlNode cell3 = listCell[5 - delN];
                if (cell3 != null && cell3.InnerText != "")
                {
                    AddCategory(cell3.InnerText, model2);
                    context.Response.Write(" - " + cell3.InnerText);
                }
                context.Response.Write(" " + allCount .ToString()+ "<br>");
            }
            context.Response.Write(allCount.ToString());
        }
        int AddCategory(string Name_, Model.Category FatherModel_)
        {
            Model.Category modelNew = new Model.Category();
            modelNew.Name = Name_;
            modelNew.FatherCategoryID =DataTypePlus.requestInt( FatherModel_.id);
            //Rank
            List<Model.Category> listModelCategory = bll.GetModelList("FatherCategoryID=" + DataTypePlus.requestInt(FatherModel_.id) + " order by Rank desc");
            if (listModelCategory != null && listModelCategory.Count > 0) modelNew.Rank = listModelCategory[0].Rank + 1;
            else modelNew.Rank = 1;
            //Number
            modelNew.Number = modelNew.Rank.ToString().PadLeft(2, '0');
            if (modelNew.FatherCategoryID > 0) modelNew.Number = FatherModel_.Number + modelNew.Number;
            modelNew.CategoryID_Tree = "";
            int id_new = bll.Add(modelNew);
            //更新idTree
            modelNew = bll.GetModel(id_new);
            modelNew.CategoryID_Tree = id_new + "|";
            if (modelNew.FatherCategoryID > 0) modelNew.CategoryID_Tree = FatherModel_.CategoryID_Tree + modelNew.CategoryID_Tree;
            bll.Update(modelNew);
            allCount++;
            return id_new;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}